package com.cw.controller;


import com.cw.common.api.ResultObject;
import com.cw.common.constant.Constants;
import com.cw.dto.UserRoleInsertDto;
import com.cw.entity.SysRole;
import com.cw.entity.SysUser;
import com.cw.entity.SysUserRole;
import com.cw.service.ISysRoleService;
import com.cw.service.ISysUserRoleService;
import com.cw.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author smq
* @since 2021-04-28
*/
@Api(tags = {"用户角色关系"})
@RestController
@RequestMapping("/sys-user-role")
public class SysUserRoleController {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 添加用户角色
     * @return
     */
    @ApiOperation(value = "用户添加角色")
    @PostMapping("/addUserRole")
    public int addUserRole(@RequestBody UserRoleInsertDto userRoleInsertDto) {
        //String userName=map.get("userName").toString();
        String roleId = userRoleInsertDto.getRoleId();
        List<String> userIds = userRoleInsertDto.getUserId();
        List<SysUserRole> sysUserRoleList = new ArrayList<>();

        for (String userId : userIds) {
            SysUserRole sysUserRole = new SysUserRole();
            //角色Id
            sysUserRole.setRoleId(roleId);
            //List<SysUser> sysUser = sysUserService.getSysUser(null, userName);
            //用户Id
            sysUserRole.setUserId(userId);
            sysUserRoleList.add(sysUserRole);
        }
        int insertSysUserRole = sysUserRoleService.insertSysUserRole(sysUserRoleList);
        if (insertSysUserRole > 0) {
            return Constants.success;
        } else {
            return Constants.fail;
        }
    }

    /**
     * 查询用户角色
     *
     * @return
     */
    @ApiOperation(value = "查询用户角色", notes = "查询用户角色", tags = {"query"})
    @ApiImplicitParam(name = "userName", value = "用户名", required = true, dataType = "String")
    @RequestMapping(value = "/getUserRole", method = RequestMethod.POST)
    public SysRole getUserRole(@RequestParam("userName") String userName) {

        List<SysUser> sysUser = sysUserService.getSysUser(null, userName);
        //用户Id
        String userId = sysUser.get(0).getUserId();
        List<SysUserRole> sysUserRole = sysUserRoleService.getSysUserRole(userId);
        //角色Id
        String roleId = sysUserRole.get(0).getRoleId();
        List<SysRole> sysRole = sysRoleService.getSysRole(roleId, null, null);
        return sysRole.get(0);
    }

    /**
     * 修改用户角色
     *
     * @return
     */
    @ApiOperation(value = "用户修改角色", notes = "用户修改角色", tags = {"update"})
    @ApiImplicitParam(name = "map", value = "用户对象", required = true, dataType = "Map<String, Object>")
    @RequestMapping(value = "/updateUserRole", method = RequestMethod.POST)
    public int updateUserRole(@RequestBody Map<String, Object> map) {
        String roleId = map.get("roleId").toString();
        String userId = map.get("userId").toString();
        SysUserRole sysUserRole = new SysUserRole();
        //角色Id
        sysUserRole.setRoleId(roleId);
        //List<SysUser> sysUser = sysUserService.getSysUser(null, userName);
        //用户Id
        sysUserRole.setUserId(userId);
        int updateSysUserRole = sysUserRoleService.updateSysUserRole(sysUserRole);
        if (updateSysUserRole > 0) {
            return Constants.success;
        } else {
            return Constants.fail;
        }
    }



    /**
     * 删除用户对应角色
     *
     * @return
     */
    @ApiOperation("通过用户登录名删除这位用户的角色")
    @DeleteMapping("/deleteUserRole")
    public ResultObject deleteUserRole(@RequestParam("userName") String userName, String roleId) {
        List<SysUser> sysUser = sysUserService.getSysUser(null, userName);
        //用户Id
        String userId = sysUser.get(0).getUserId();
        int deleteSysUserRole = sysUserRoleService.deleteSysUserRole(userId,roleId);
        if (deleteSysUserRole > 0) {
            sysUserService.updateSysUserSetRoleCodeNull(userId);
            return ResultObject.successReturn(Constants.success);
        } else {
            return ResultObject.failed("出现BUG了");
        }
    }

    @ApiOperation("通过角色主键获取当前角色所有用户")
    @GetMapping("/getSysUserByRoleId")
    public ResultObject getSysUserByRoleId(@RequestParam String roleId) {
        return ResultObject.successReturn(sysUserRoleService.getUserByRoleId(roleId));
    }
}

